package mes.kanban.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import mes.kanban.entity.MesConfigList;
import mes.kanban.service.ConfigListI;
import mes.kanban.service.MaterialServiceI;
import mes.kanban.service.PollingConfigServiceI;
import mes.kanban.vo.AjaxReturnVO;
import mes.kanban.vo.MesConfigListVO;
import mes.kanban.vo.MesMaterialVO;
import mes.kanban.vo.MesPollingConfigVO;


/**
 * 配置详情
 * @author gjy
 *
 * 2018年1月26日下午3:42:23
 */
@Controller
@RequestMapping(value="/config")
public class ConfigListController {
	
	private static final Logger log = LoggerFactory.getLogger(ConfigListController.class);
	
	@Autowired
	private ConfigListI configService;
	@Autowired
	private PollingConfigServiceI pollingConfigService;
	@Autowired
	private MaterialServiceI materialServiceI;
	
	
	/**
	 * 根据看板Id跳转到相应的详情列表
	 * @param model
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/toConfigListByPoolingConfigId")
	public String selectConfigListByReportConfigId(Model model,@RequestParam(value="id")String id) {
	
		MesPollingConfigVO pollingConfig = pollingConfigService.findConfigByIdForShow(id);
		model.addAttribute("pollingConfig",pollingConfig);
		model.addAttribute("configId",id);
		
		return "configList/list";
	}
	
	/**
	 * 根据看板Id查询看板对应的详细配置
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/getConfigListByPoolingConfigId")
	@ResponseBody
	public AjaxReturnVO getConfigListByPollingConfigId(@RequestParam(value="id")String id) {
		log.info("查询看板详细配置信息");
		AjaxReturnVO vo = new AjaxReturnVO();
		try {
			//根据看板id查询看板详细配置
			List<MesConfigListVO> configLists = configService.getConfigListByPolingConfigIdAndSO(id);
			//查询配置关联的素材
			for(MesConfigListVO config : configLists) {
				MesMaterialVO materialVo = materialServiceI.findMaterialById(config.getMaterialId());
				config.setMaterialName(materialVo.getMaterialName());
				config.setMaterialRealName(materialVo.getName());
			}
			vo.setData(configLists);
			vo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("请求失败");
			vo.setOk(false);
		}
		return vo;
	}
	
	/**
	 * 保存修改
	 * @param config
	 * @return
	 */
	@PostMapping(value="/edit")
	@ResponseBody
	public AjaxReturnVO editConfig(@RequestBody MesConfigList config) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			configService.updateConfig(config);
			j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
		
		return j;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@GetMapping(value="/delete")
	@ResponseBody
	public AjaxReturnVO delConfig(String id) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			configService.deleteConfig(id);
			j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setOk(false);
		}
		
		return j;
		
	}
	
	
	/**
	 * 保存新增
	 * @param model
	 * @param configs
	 * @return
	 */
	@PostMapping(value="/add")
	@ResponseBody
	public AjaxReturnVO addConfig(Model model,@RequestBody List<MesConfigList> configs) {
		
		AjaxReturnVO j = new AjaxReturnVO();
		
		try {
			for (MesConfigList configList2 : configs) {
				configService.addConfig(configList2);
			}
			j.setMsg("success");
			j.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			j.setMsg("fail");
			j.setOk(false);
		}
		
		return j;
	}
	
	/**
	 * 配置发布
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/configPushById")
	@ResponseBody
	public AjaxReturnVO configPushById(@RequestParam(value="id")String id) {
		log.info("配置发布");
		AjaxReturnVO vo = new AjaxReturnVO();
		try {
			configService.configPushById(id);
			vo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("请求失败");
			vo.setOk(false);
		}
		return vo;
	}
	
	/**
	 * 配置撤回
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/configRejectById")
	@ResponseBody
	public AjaxReturnVO configRejectById(@RequestParam(value="id")String id) {
		log.info("配置撤回");
		AjaxReturnVO vo = new AjaxReturnVO();
		try {
			configService.configRejectById(id);
			vo.setOk(true);
		} catch (Exception e) {
			e.printStackTrace();
			vo.setMsg("请求失败");
			vo.setOk(false);
		}
		return vo;
	}

}
